package cn.cqsztech.slidingwindow;

/**
 * 通过滑动窗口找最大平均值
 */
public class FindMaxAverage {
    public static double findMaxAverage(int[] nums, int k) {
        double result = 0 ;
        int sum = 0;
        //计算前k个值的和
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        result = (double) sum / k;
        for (int i = k; i <nums.length; i++) {
            //滑动 去掉最窗口左边的 加入窗口右边的
            sum  = sum + nums[i] -nums[i-k];
            //找最大值
            result = Math.max(result, (double) sum/k);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {13,21,23,4,5,6,7,19,8,21,4,21,1,3};
        System.out.println(findMaxAverage(nums, 3));
    }
}
